import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

  const routes = [
  { path: '/', redirect:'/login' },
  { path: '/login', component: () => import('../views/login/index.vue') }
  ]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局前置路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  // 如果没有登录跳转到登陆页面
  if (!token) return next('/login')
  next()
})

export default router
